<?php
class Admin_SubjectsController extends Zend_Controller_Action {
	
     public function init(){
		Zend_Loader::loadClass('SubjectsModel');
	   Zend_Loader::loadClass('InstitutesModel');
       Zend_Loader::loadClass('RoomModel');
		Zend_Loader::loadClass('LoginModel');
        Zend_Loader::loadClass('TeacherModel');
        Zend_Loader::loadClass('SemesterModel');
	}
	
    function preDispatch(){
    	/*
    	 * Kiem tra dang nhap
    	 */
		
		$auth = Zend_Auth::getInstance();
		if(strcmp($this->_request->getActionName(),'login')!=0){
			if (!$auth->hasIdentity()) {
				$this->_redirect('/admin/index/login');
			}
		}
		/*
		 * Tra cac thong tin ve moduln cho view
		*/
		$this->view->module = $this->_request->getModuleName();
		$this->view->controller =  $this->_request->getControllerName();
		$this->view->action =  $this->_request->getActionName();
		/*
		 * Load template
		*/
		$layoutPath = APPLICATION_PATH  . '/templates/admin/admin';
		$option = array (	'layout' 		=> 'index',
				'layoutPath' 	=> $layoutPath );
		Zend_Layout::startMvc ( $option );
	}
	
	function indexAction(){
	    
    	if($this->_request->isPost()){
		$code_search = $this->_request->getParam('code_search');
		$c_search = $this->_request->getParam('c_search');
		
        //echo $code_search.'|'.$c_search;die;
        
        $this->view->code_search = $code_search;
		$this->view->c_search = $c_search;
		
        $list = SubjectsModel::search($code_search,$c_search);
		//Zend_Debug::dump($list);die;
        
        $this->view->list = $list;
		$this->view->search = 1;
    	}
        else{
        $page = $this->_request->getParam('page');
		if(!$page) $offset = 1;
		else $offset = $page;
		$this->view->now_page = $offset;
        $this->view->count = count(SubjectsModel::getAll());
        $list = SubjectsModel::selectLimit(30,($offset-1)*30);
    	$this->view->list = $list;
        $this->view->search = 0;
        }
	}
    
    public function addAction(){
        if($this->_request->isPost()){
            
            $name= $this->_request->getParam('name');
            $code= $this->_request->getParam('code');
            $weight= $this->_request->getParam('weight');
            $id_room= $this->_request->getParam('id_room');
            $id_teachers= $this->_request->getParam('id_teachers');
            $id_semester= $this->_request->getParam('id_semester');
           
            
          
            $data= array('name'=>$name,
                         'code'=>$code,
                         'id_room'=>$id_room,
                         'id_teachers'=>$id_teachers,
                         'id_semester'=>$id_semester,
                         'weight'=>$weight,
                         
                         
            );
            
       //     Zend_Debug::dump($data);die;
            
            if($name!='' && $code!=''  && $id_room!='' && $id_semester!='' && $weight!=''){
                $model= new SubjectsModel();
                $model->insert($data);
                $this->_redirect('/admin/subjects');          
                
            }else{
                $this->view->warning = 1;
                $this->view->data= $data;
            }
        
        }
        $this->view->room = RoomModel::room();
        $this->view->semester = SemesterModel::semester();
        $this->view->teacher = TeacherModel::getAllTeacher();

    } 
    
    public function editAction(){
               if($_REQUEST['id']!=''){
            $id= $_REQUEST['id'];
            $model= new SubjectsModel();
         //   Zend_Debug::dump($model->getDbbyID($id));die;
            $this->view->data= $model->getDbbyID($id);
        }
        if(($_REQUEST['do']=='edit')&&($_REQUEST['id']!='')&&$this->_request->isPost()){
            $id= $_REQUEST['id'];
           $name= $this->_request->getParam('name');
            $code= $this->_request->getParam('code');
            $weight= $this->_request->getParam('weight');
            $id_room= $this->_request->getParam('id_room');
            $id_teachers= $this->_request->getParam('id_teachers');
            $id_semester= $this->_request->getParam('id_semester');
           
            
           $data= array('name'=>$name,
                         'code'=>$code,
                         'id_room'=>$id_room,
                         'id_teachers'=>$id_teachers,
                         'id_semester'=>$id_semester,
                         'weight'=>$weight,
                         
                         
            );
            
    
            
               if($name!='' && $code!=''  && $id_room!='' && $id_semester!='' && $weight!=''){
                $model->update($data,"id=".$id);
                $this->_redirect('/admin/subjects');          
                
            }else{
                $this->view->warning = 1;
            }
        
        }
        $this->view->room = RoomModel::room();
        $this->view->semester = SemesterModel::semester();
        $this->view->teacher = TeacherModel::getAllTeacher();
    }
    
    public function delAction(){
         $dt = new SubjectsModel;
    	$request = $this->getRequest();
    	$id = $request->getParam('id');
    	if($id){
    		$dt->delete("id=".$id);
    	}
    	exit();
    }
	
   
}